package com.example.aoptest1.springAOP;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.concurrent.ForkJoinTask;

/**
 *@描述 定义切面
 *@参数
 *@返回值
 */
@Aspect
@Component
public class LogAOP {


    @Pointcut("execution(* com.example.aoptest1.service.*.*(..))")
    public  void pointcut()
    {

    }
    @Before("pointcut()")
    public void before(JoinPoint joinPoint)
    {
        System.out.println("----before------执行到了logaop-----");
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        System.out.println("-----------获取类名--" + className);
        System.out.println(className);
        System.out.println("-----------获取方法名--" + methodName);
        System.out.println(methodName);


        MethodSignature methodSignature=(MethodSignature) joinPoint.getSignature();
        String[] parameterNames=methodSignature.getParameterNames();

        Object[] args=joinPoint.getArgs();
        System.out.println("开始打印参数============");

        for (int i=0;i<parameterNames.length;i++)
        {
            System.out.println(parameterNames[i] +":"+ args[i]);
        }
        System.out.println("打印结束 ============");
    }

    @After("pointcut()")
    public void after()
    {
        System.out.println("======after=======");
    }

//    @AfterReturning(pointcut = "pointcut()",returning = "objects")
//    public Objects afterR(Objects objects)
//    {
//        System.out.println("afterR ==== 输出结果========");
//        //System.out.println("切面收到的返回值 :" + rs);
//        //return rs;
//        return objects;
//    }


   // @AfterReturning(value = "execution(* com.example.aoptest1.service.*.*(..))")

    @AfterReturning(pointcut = "pointcut()",returning = "rvt")
    public void afterR(String rvt){
        System.out.println("afterR ==== 输出结果========" + rvt);
        //System.out.println("切面收到的返回值 :" + rs);
        //return rs;

    }
@AfterThrowing(value = "pointcut()",throwing = "throwable")
 public void afterThrow(Throwable throwable)
 {
     System.out.println("--AfterThrowable--------");
 }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

            Object proceed = joinPoint.proceed();


        System.out.println("=====around========");
        return null;
    }

}
